﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using AutoMapper;

namespace Data {
    public class ChiTietPhieuNhapRepository {
        /// <summary>
        /// them chi tiet phieu nhap
        /// </summary>
        /// <param name="chiTietList"></param>
        /// <returns>true neu thanh cong false neu that bai</returns>
        public bool InsertChiTietPhieuNhap(List<ChiTietPhieuNhapDTO> chiTietList) {
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
            foreach (ChiTietPhieuNhapDTO item in chiTietList) {
                CHITIETPHIEUNHAP chiTietEF = new CHITIETPHIEUNHAP();
                Mapper.CreateMap<ChiTietPhieuNhapDTO, CHITIETPHIEUNHAP>();
                chiTietEF = Mapper.Map<ChiTietPhieuNhapDTO, CHITIETPHIEUNHAP>(item);

                context.AddToChiTietPhieuNhapList(chiTietEF);

                //update so luong ton
                SACH s = (from sach in context.SachList
                         where sach.IDSach == chiTietEF.IDSach
                         select sach).First();

                s.SoLuongTon = s.SoLuongTon + chiTietEF.SoLuong;
            }
            context.SaveChanges();
            return true;
        }
    }
}
